﻿using System;
using System.Text;
using TI_BLE_HCI_ClientLib.GenericHCIEntities;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;

namespace TI_BLE_HCI_ClientLib.HCIEvents
{
    public class HCIEvent_ATT_Handle_Value_Notification : HCIEvent_GATT_Base_Event
    {
        public ushort CharacteristicHandle
        {
            get { return (ushort)(base.Pdu[0] + (base.Pdu[1] << 8)); }
        }

        public byte[] Value
        {
            get
            {
                byte[] value = new byte[base.Pdu.Length - 2];

                Array.Copy(base.Pdu, 2, value, 0, value.Length);

                return value;
            }
        }

        public string ValueAsString
        {
            get
            {
                try
                {
                    return new string(Encoding.UTF8.GetChars(this.Value));
                }
                catch
                {
                    return null;
                }
            }
        }

        public bool ValueAsBoolean
        {
            get
            {
                switch (base.PduLen)
                {
                    case 3:
                        return this.Value[0] != 0;
                }

                throw new ArgumentException("PduLen!=3");
            }
        }

        public byte ValueAsUInt8
        {
            get
            {
                switch (base.PduLen)
                {
                    case 3:
                        return this.Value[0];
                }

                throw new ArgumentException("PduLen!=3");
            }
        }

        public ushort ValueAsUInt16
        {
            get
            {
                switch (base.PduLen)
                {
                    case 4:
                        return (ushort)(this.Value[0] + (this.Value[1] << 8));
                }

                throw new ArgumentException("PduLen!=4");
            }
        }

        public int ValueAsInt32
        {
            get
            {
                return BitConverter.ToInt32(this.Value,0);
            }
        }

        public short ValueAsInt16
        {
            get
            {
                return BitConverter.ToInt16(this.Value, 0);
            }
        }

        public float ValueAsSingle
        {
            get
            {
                return BitConverter.ToSingle(this.Value, 0);
            }
        }

        public double ValueAsDouble
        {
            get
            {
                return BitConverter.ToDouble(this.Value, 0);
            }
        }

        public HCIEvent_ATT_Handle_Value_Notification(HCIVendorSpecificEventHostErrorCode status,
            ushort connHandle, byte pduLen, byte[] pdu)
            : base(status, connHandle, pduLen, pdu)
        {
        }
    }
}
